//
//  SLUITips.m
//  shunlianPower2
//
//  Created by 陈镇 on 2019/1/18.
//  Copyright © 2019年 顺联网络. All rights reserved.
//

#import "SLUITips.h"
#import <QMUIKit/QMUIToastContentView.h>
@implementation SLUITips

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
 
 #define SLTips(text,view) [QMUITips showWithText:text inView:view hideAfterDelay:1]
 #define SLTipToWindow(s) SLTips(s,SLWindow)
 #define SLTipCurrenView(text) SLTips(text,CurrentVC.view);
 
*/
- (instancetype)initWithFrame:(CGRect)frame{
    if (self  = [super initWithFrame:frame]) {
        self.userInteractionEnabled = NO;
    }
    return self;
}

+(QMUITips *)showWithText:(NSString *)text inView:(UIView *)view hideAfterDelay:(NSTimeInterval)delay{
    UIView *contentView = view;
    if (!contentView) {
        contentView = [UIApplication sharedApplication].keyWindow;
    }
    if (!contentView) {
        return nil;
    }
    QMUITips *tips = [super showWithText:text inView:contentView hideAfterDelay:delay];
    tips.userInteractionEnabled = NO;
    return tips;
}

+(QMUITips *)showSucceed:(NSString *)text{
    QMUITips *tips = [super showSucceed:text];
    tips.userInteractionEnabled = NO;
    return tips;
}



+(QMUITips *)sl_showWithText:(NSAttributedString *)text inView:(UIView *)view hideAfterDelay:(NSTimeInterval)delay{
    UIView *contentView = view;
    if (!contentView) {
        contentView = [UIApplication sharedApplication].keyWindow;
    }
    if (!contentView) {
        return nil;
    }
    
    SLUITips *tips = [[SLUITips alloc] initWithView:view];
    [view addSubview:tips];
    tips.removeFromSuperViewWhenHide = YES;
    tips.userInteractionEnabled = NO;
    [tips sl_showTipWithText:text detailText:nil hideAfterDelay:delay];
    return tips;
}



- (void)sl_showTipWithText:(NSAttributedString *)text detailText:(NSAttributedString *)detailText hideAfterDelay:(NSTimeInterval)delay {
    
    QMUIToastContentView *contentView = (QMUIToastContentView *)self.contentView;

    
    contentView.textLabel.attributedText = text?text: nil;
    contentView.detailTextLabel.attributedText = detailText?detailText:nil;
    
    [self showAnimated:YES];
    
    if (delay == QMUITipsAutomaticallyHideToastSeconds) {
        [self hideAnimated:YES afterDelay:[QMUITips smartDelaySecondsForTipsText:text.string]];
    } else if (delay > 0) {
        [self hideAnimated:YES afterDelay:delay];
    }
}

+(UIWindow *)getRootWindow{
    
    NSArray *windows = [[UIApplication sharedApplication] windows];
    for(UIWindow *window in [windows reverseObjectEnumerator]) {
        if ([window isKindOfClass:[UIWindow class]] &&
            window.windowLevel == UIWindowLevelNormal &&
            CGRectEqualToRect(window.bounds, [UIScreen mainScreen].bounds))
            return window;
    }
    return [UIApplication sharedApplication].keyWindow;
    
}
@end
